﻿<%@ WebHandler Language="C#" Class="Catalog" %>

using System;
using System.Web;
using System.Collections.Generic;
using Company.StudyClub.Model;
using Company.StudyClub.BLL.Course;
using Company.StudyClub.BLL.Common;
using System.Text;

public class Catalog : IHttpHandler {
    
    private HttpHelper helper = new HttpHelper();
    
    public void ProcessRequest (HttpContext context) {
        HttpResponse response = context.Response;

        switch (helper.GetString("type"))
        {
            case "addcatalog":
                AddCatalog(response);
                break;
            case "getcatalogs":
                GetCatalogs(response);
                break;
            case "editcatalog":
                EditCatalog(response);
                break;
            case "deletecatalog":
                DeleteCatalog(response);
                break;
            case "sorting":
                DoSorting(response);
                break;            
            default: break;
        }
    }

    private void AddCatalog(HttpResponse response)
    {
        try
        {
            Course_Catalog model = new Course_Catalog();
            model.CatalogName = helper.GetString("cname");
            model.CreateTime = DateTime.Now;
            model.CreatorID = helper.CurrentUser.UserID;
            model.ParentID = helper.GetInt("pid");            

            CatalogBLL objBLL = new CatalogBLL();            
            model.CatalogID = objBLL.Add(model);

            if (model.ParentID > 0)
            {
                //update totalsubs
                objBLL.UpdateTotalSubs(model.ParentID, 1);
                Course_Catalog pmodel = objBLL.GetModel(model.ParentID);
                model.KeyString = pmodel.KeyString + model.CatalogID + "/";
            }
            else
            {
                model.KeyString = "/" + model.CatalogID + "/";
            }
            
            //update keystring
            objBLL.Update(model);

            response.Write(model.CatalogID.ToString());
        }
        catch
        {
            response.Write("-1");
        }
    }

    private void GetCatalogs(HttpResponse response)
    {
        CatalogBLL objBLL = new CatalogBLL();
        
        List<Course_Catalog> objList = objBLL.GetCatalogsByParentID(helper.GetInt("pid"));

        StringBuilder objSb = new StringBuilder();

        objSb.Append("[");
        for (int i = 0; i < objList.Count; i++)
        {
            Course_Catalog model = (Course_Catalog)objList[i];
            
            objSb.Append("{\"cid\":\"");
            objSb.Append(model.CatalogID);
            objSb.Append("\",\"cname\":\"");
            objSb.Append(model.CatalogName);
            objSb.Append("\",\"totalsubs\":\"");
            objSb.Append(model.TotalSubs);
            objSb.Append("\",\"totalcourses\":\"");
            objSb.Append(model.TotalCourses);
            objSb.Append("\",\"sequence\":\"");
            objSb.Append(model.Sequence);
            objSb.Append("\",\"pid\":\"");
            objSb.Append(model.ParentID);
            objSb.Append("\",\"ctime\":\"");
            objSb.Append(model.CreateTime);
            objSb.Append("\"}");
            if (i != objList.Count - 1)
            {
                objSb.Append(",");
            }
        }
        objSb.Append("]");

        response.Write(objSb.ToString());        
    }

    private void EditCatalog(HttpResponse response)
    {
        int opid = helper.GetInt("opid");
        int pid  = helper.GetInt("pid");
        int cid  = helper.GetInt("cid");
        
        CatalogBLL objBLL = new CatalogBLL();
        
        string newkeystring = string.Empty;
       
        if (pid > 0)
        {
            Course_Catalog pmodel = objBLL.GetModel(pid);

            if (pmodel.KeyString.IndexOf("/" + cid + "/") >= 0)
            {
                response.Write("-1");
                return;
            }
            newkeystring = pmodel.KeyString;
        }
        
        Course_Catalog model = objBLL.GetModel(cid);

        if (model == null)
        {
            response.Write("0");
            return;
        }
        
        model.CatalogName = helper.GetString("cname");

        if (pid != model.ParentID)
            model.ParentID = pid;
        
        objBLL.Update(model);
        
        if (pid != opid)
        {
            objBLL.UpdateTotalSubs(pid, 1);
            objBLL.UpdateTotalSubs(opid, -1);
            
            //update keystring
            if (newkeystring.Length == 0)
            {
                newkeystring = "/" + model.CatalogID + "/";
            }
            else
            {
                newkeystring += model.CatalogID + "/";
            }
            
            bool result = objBLL.UpdateKeyString(model.KeyString, newkeystring);

            if (!result)
            {
                response.Write("2");
                return;
            }            
        }

        response.Write("1");
    }

    private void DeleteCatalog(HttpResponse response)
    {
        try
        {
            string[] cids = helper.GetString("cids").Split(',');

            for (int i = 0; i < cids.Length; i++)
            {
                if (cids[i] == string.Empty)
                    continue;

                CatalogBLL objBLL = new CatalogBLL();

                int catalogId = int.Parse(cids[i]);
                Course_Catalog model = objBLL.GetModel(catalogId);

                if (model.TotalCourses == 0 && model.TotalSubs == 0)
                {
                    objBLL.Delete(catalogId);
                    objBLL.UpdateTotalSubs(helper.GetInt("pid"), -1);
                }
            }
            response.Write("1");
        }
        catch
        {
            response.Write("0");
        }    
        
    }

    private void DoSorting(HttpResponse response)
    {      
        CatalogBLL objBLL = new CatalogBLL();

        try
        {
            string[] cids = helper.GetString("cids").Split(',');

            for (int i = 0; i < cids.Length; i++)
            {
                if (cids[i] == string.Empty)
                    continue;

                int cid = int.Parse(cids[i]);
                                
                objBLL.UpdateSequence(cid, i * 10);
            }
            response.Write("1");
        }
        catch
        {
            response.Write("0");
        }    
    }   

    public bool IsReusable
    {
        get {
            return false;
        }
    }

}